import six

from scrapy.utils.misc import load_object
from . import defaults


"""
根据setting中配置实例化redis连接。被dupefilter和scheduler调用
"""

SETTINGS_PARAMS_MAP = {
    'REDIS_URL': 'url',
    'REDIS_HOST': 'host',
    'REDIS_PORT': 'port',
    'REDIS_ENCODING': 'encoding',
}


def get_redis_from_settings(settings):
    params = defaults.REDIS_PARAMS.copy()
    params.update(settings.getdict('REDIS_PARAMS'))
    for source, dest in SETTINGS_PARAMS_MAP.items():
        val = settings.get(source)
        if val:
            params[dest] = val
    # Allow ``redis_cls`` to be a path to a class.
    if isinstance(params.get('redis_cls'), six.string_types):
        params['redis_cls'] = load_object(params['redis_cls'])

    return get_redis(**params)


#向后兼容的别名
from_settings = get_redis_from_settings


def get_redis(**kwargs):
    """
    Returns a redis client instance.
    """
    redis_cls = kwargs.pop('redis_cls', defaults.REDIS_CLS)
    url = kwargs.pop('url', None)
    if url:
        #实例化defaults.REDIS_CLS类
        return redis_cls.from_url(url, **kwargs)
    else:
        return redis_cls(**kwargs)
